(function (window, undefined) {
    class Game {
        constructor(tage, score) {
            this.food = new Food({})
            this.food = new Food({})
            this.food = new Food({})
            this.snake = new Snake({})
            this.tage = tage
            this.score = score
        }
        start() {
            this.food.render(this.tage, this.food.position())
            this.food.render(this.tage, this.food.position())
            this.food.render(this.tage, this.food.position())
            this.snake.render(this.tage)
            this.runSnake()
        }
        runSnake() {
            const timer = setInterval(function () {

                this.snake.moveSnake()
                if (this.isEnd(this.tage)) {
                    clearInterval(timer)
                }
                // this.moreLong()
                this.snake.removeSnake(this.tage)
                this.snake.render(this.tage)
                this.keyCode()
            }.bind(this), 150)
            // console.log(this.snake.direction);
        }
        keyCode() {
            document.onkeydown = function (e) {
                switch (e.keyCode) {
                    case 87:
                        this.snake.direction = "top"
                        break;
                    case 65:
                        this.snake.direction = "left"
                        break;
                    case 83:
                        this.snake.direction = "bottom"
                        break;
                    case 68:
                        this.snake.direction = "right"
                        break;
                }
            }.bind(this)
        }
        moreLong() {
            for (let i = this.food.bodys.length - 1; i >= 0; i--) {
                if (this.food.bodys[i].offsetTop === this.snake.eles[0].top * this.snake.height && this.food.bodys[i].offsetLeft === this.snake.eles[0].left * this.snake.width) {
                    this.food.removeFood(this.tage, i)
                    this.snake.eles.push({
                        left: this.snake.eles[this.snake.eles.length - 1].left,
                        top: this.snake.eles[this.snake.eles.length - 1].top,
                        color: this.snake.eles[this.snake.eles.length - 1].color
                    })
                    this.food.render(this.tage, this.food.position())
                    this.addOne()
                    // console.log(this.snake.eles);
                }
            }
        };
        addOne () {
            this.score.innerHTML = parseInt(this.score.innerText) + 1
        }

        isEnd(parent) {
            if (this.snake.eles[0].top < 0 || this.snake.eles[0].top >= parent.offsetHeight / this.snake.height || this.snake.eles[0].left >= parent.offsetWidth / this.snake.width || this.snake.eles[0].left < 0) {
                this.tage.classList = "tage show1"
                return true
            }
            for (let i = 1; i < this.snake.eles.length; i++) {
                if (this.snake.eles[i].left === this.snake.eles[0].left && this.snake.eles[i].top === this.snake.eles[0].top) {
                    this.tage.classList = "tage show2"
                    return true
                }
            }
            this.moreLong()
        };
    }
    window.Game = Game
})(window, undefined);
